﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class AdjustTimeSheetDialog : ChildWindow
    {
        public int currentEmployeeID;
        DateTime currentDate;

        public AdjustTimeSheetDialog()
        {
            InitializeComponent();
        }

        public async void init(int employeeID, DateTime selectedDate)
        {
            // Load punches
            lbPunches.Items.Clear();
            await Globals.db.GetTimeSheetObjects("EmployeeID="+employeeID.ToString()+" and PunchDateTime>='"+
                selectedDate.ToShortDateString() + "' and PunchDateTime<'" + selectedDate.AddDays(1).ToShortDateString() +
                "' and ISNULL(Adjusted,0)=0 order by PunchDateTime");
            // Fill the list with punch information
            foreach (ServiceReferenceOpenRPOS.TimeSheet ts in Globals.db.TimeSheetRecords)
            {                
                string strPunch = ((ts.col_PunchType) ? " IN: " : "OUT: ") + ((ts.col_PunchDateTime.Hour%12<10)?" ":"") +
                    ts.col_PunchDateTime.ToShortTimeString()+(ts.col_Adjustment?" (A)":"");
                lbPunches.Items.Add(strPunch);
            }
            currentDate = selectedDate;
            currentEmployeeID=employeeID;
            Globals.showWaitCursor(false);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            // Re-initialize the time sheet grid... 
            Globals.timeSheet.initTimeSheetGrid(true);
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            // Re-initialize the time sheet grid... 
            Globals.timeSheet.initTimeSheetGrid(true);
            this.DialogResult = false;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddPunchDialog apd = new AddPunchDialog();
            apd.atsd = this;
            apd.init();
            apd.tpPunchTime.Value = currentDate;
            apd.rbIn.IsChecked = true;
            apd.Show();
        }

        private async void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            // Prompt the user - verify that the punch should be removed
            if (lbPunches.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Are you sure you want to delete the selected punch?", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    // Mark the punch as "Adjusted"
                    Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].bFilterAdjusted = true;
                    Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].bFilterAdjustedByEmployeeID = true;
                    Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].col_Adjusted = true;
                    Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].col_AdjustedByEmployeeID = Globals.currentEmployee.col_ID;
                    await Globals.db.UpdateTimeSheetObject(Globals.db.TimeSheetRecords[lbPunches.SelectedIndex]);
                    init(currentEmployeeID, Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].col_PunchDateTime);
                }
            }
        }

        private void btnAdjust_Click(object sender, RoutedEventArgs e)
        {
            if (lbPunches.SelectedIndex >= 0)
            {
                // Adjust the selected punch
                AdjustPunchDialog apd = new AdjustPunchDialog();
                apd.atsd = this;
                apd.init();
                apd.adjustedTimeSheet = Globals.db.TimeSheetRecords[lbPunches.SelectedIndex];
                apd.tpPunchTime.Value = Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].col_PunchDateTime;
                apd.rbIn.IsChecked = Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].col_PunchType == true;
                apd.rbOut.IsChecked = !apd.rbIn.IsChecked;
                if (apd.rbIn.IsChecked == true)
                    apd.cmbRole.SelectedValue = Globals.db.RoleRecords.Where(x => x.col_ID == Globals.db.TimeSheetRecords[lbPunches.SelectedIndex].col_RoleID).First().col_Title;
                apd.Show();
            }
        }
    }
}

